package com.alisls.shequtao.service.goods.web;

import com.alisls.shequtao.service.goods.dto.CategoryDTO;
import com.alisls.shequtao.service.goods.service.CategoryService;
import com.alisls.shequtao.service.goods.vo.CategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 商品分类管理
 *
 * @author Ke Wang
 */
@Api(tags = "商品分类管理")
@RestController
@RequestMapping("/category")
@AllArgsConstructor
@Slf4j
public class CategoryController {

    private final CategoryService categoryService;

    @ApiOperation(value = "根据categoryIds查询商品分类")
    @GetMapping("/listCategoriesByIds")
    public ResponseEntity<List<CategoryDTO>> listCategoriesByIds(@RequestParam(required = true) List<Long> categoryIds) {
        List<CategoryDTO> categories = categoryService.listCategoriesByIds(categoryIds);
        return ResponseEntity.ok(categories);
    }

    @ApiOperation(value = "根据parentId查询商品分类")
    @GetMapping("/listCategoriesByParentId")
    public ResponseEntity<List<CategoryVO>> listCategoriesByParentId(@RequestParam Long parentId) {
        return ResponseEntity.ok(categoryService.listCategoriesByParentId(parentId));
    }

    @ApiOperation(value = "根据categoryIds查询商品分类")
    @GetMapping("/tree")
    public ResponseEntity<List<CategoryVO>> tree() {
        return ResponseEntity.ok(categoryService.listCategoriesByTree(null));
    }

    @ApiOperation(value = "保存商品分类")
    @PostMapping("/save")
    public ResponseEntity<CategoryDTO> save(@RequestBody @Valid CategoryDTO categoryDTO) {
        categoryService.save(categoryDTO);
        return ResponseEntity.ok(categoryDTO);
    }

}
